package com.kkm.controller;

import com.kkm.pojo.Consumer;
import com.kkm.pojo.PageBean;
import com.kkm.service.Imp.UserServiceImp;
import com.kkm.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@WebServlet("/consumer")
public class UserServlet extends BaseServlet {
    private UserService service=new UserServiceImp();
    //展示用户
    public void showUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
       // req.removeAttribute("currPageMax");
        PageBean<Consumer> pageBeanUser=service.showUser(currentPage,rows);
        req.setAttribute("Consumer",pageBeanUser);
        req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
    }

   //模糊查询用户
    public void checkUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取查询条件，封装成集合
        Map<String, String[]> condition = req.getParameterMap();
        String currentPage=req.getParameter("currentPage");
        int rows=8;
        req.removeAttribute("currPageMax");
        PageBean<Consumer> userList=service.checkUser(condition,currentPage,rows);
        if(userList==null){
//            userList=service.showUser(currentPage,rows); 如果需要在查询不到时显示全部数据
            req.setAttribute("Consumer",userList);
            req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
        }else {
            req.setAttribute("Consumer",userList);
            req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
        }

    }
     //删除用户
    public void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid =req.getParameter("id");
        service.deleteUser(uid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Consumer> pageBeanUser=service.showUser(currentPage,rows);
        req.setAttribute("Consumer",pageBeanUser);
        req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
    }

    //修改用户（第一步接收信息）
    public void userById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid=req.getParameter("id");
        Consumer consumer =service.userById(uid);
        req.setAttribute("consumer", consumer);
        req.getRequestDispatcher("/modifyUser.jsp").forward(req,resp);
    }
    //修改用户
    public void modifyUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        String uid=req.getParameter("uid");
        String username=req.getParameter("username");
        String sex=req.getParameter("sex");
        String birthDate=req.getParameter("birth");
        String phone_num=req.getParameter("phone_num");
        String email=req.getParameter("email");
        String province=req.getParameter("province");
        String city=req.getParameter("city");
        String area =req.getParameter("area");
        String address=province+"-"+city+"-"+area;
        System.out.println(uid+" "+username+" "+sex+" "+birthDate+" "+phone_num+" "+email+" "+address);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date birth=sdf.parse(birthDate);
        System.out.println(birth);
        int sexId=sex=="男" ? 0:1;
        service.modifyUser(uid,username,sexId,birth,phone_num,email,address);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<Consumer> pageBeanUser=service.showUser(currentPage,rows);
        req.setAttribute("Consumer",pageBeanUser);
        req.getRequestDispatcher("/showUser.jsp").forward(req,resp);
    }
}
